import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import moment from "moment";
import * as request from "../../../../services/CommonHttpService";
import {orderGetName, orderGetColor} from "../../../../models/OrderStatus";

export default {
  namespace: 'order',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      const p = {
        ...payload,
        baseUrl: getBaseUrl(),
        process: (records) => {
          return records.map(record => {

            const {orderStatus, returnStatus} = record;

            return {
              ...record,
              paymentTime: !!record.paymentTime ? moment(record.paymentTime).format('YYYY-MM-DD HH:mm:ss'): "",
              payment: record.payment/100,
              postFee: record.postFee/100,
              orderStatus: orderGetName(orderStatus, returnStatus),
              orderStatusColor: orderGetColor(orderStatus),
            };
          });
        }
      }

      yield put({
        type: 'baseGetPageInfo',
        payload: p
      });
    },

    // 发货
    * send({payload}, {call, put, select}){

      const {value, success, fail, errorMsg, notice, server} = payload;
      const {orderId, shippingName, shippingNameCode, shippingNameWebCode, shippingCode} = value;

      const http = request.commonPut;
      const url = `${getBaseUrl()}/${orderId}/send`;

      yield put({type: 'changing'});

      try {
        let sucess = yield call(http, url, {}, {shippingName, shippingCode, shippingNameCode, shippingNameWebCode});
        yield put({type: 'changSucess'});
        success();
      } catch (error) {
        console.error("send", error);
        yield put({type: 'changError'});
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
        fail();
      }

    }
  }

}

function getBaseUrl() {
  return "/order";
}

